package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Sc;
import service.ScService;

import java.util.List;

/**
 * 收藏表控制器
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/sc")
public class ScController {
    @Autowired
    private ScService scService;

    // 根据ID查询收藏
    @GetMapping("/{id}")
    public Sc get_sc_by_id(@PathVariable Integer id) {
        return scService.find_by_id(id);
    }

    // 查询用户的收藏
    @GetMapping("/user/{yh_id}")
    public List<Sc> get_favorites_by_user(@PathVariable Integer yh_id) {
        return scService.find_by_user_id(yh_id);
    }

    // 查询文章的收藏数
    @GetMapping("/wz/{wz_id}/count")
    public int get_favorite_count_by_wz(@PathVariable Integer wz_id) {
        return scService.count_by_wz_id(wz_id);
    }

    // 新增收藏
    @PostMapping("/add")
    public int add_sc(@RequestBody Sc sc) {
        return scService.add(sc);
    }

    // 删除收藏
    @DeleteMapping("/{id}")
    public int delete_sc(@PathVariable Integer id) {
        return scService.delete_by_id(id);
    }

    // 删除用户对文章的收藏
    @DeleteMapping("/user/{yh_id}/wz/{wz_id}")
    public int delete_by_user_and_wz(@PathVariable Integer yh_id, @PathVariable Integer wz_id) {
        return scService.delete_by_user_and_wz(yh_id, wz_id);
    }
}